// 判断类型
export const _type = (o) => {
  if (o === null) return 'null'
  if (o === undefined) return 'undefined' //兼容ie8
  var s = Object.prototype.toString.call(o)
  var t = s.match(/\[object\s(\w*?)\]/)[1].toLowerCase()
  return t === 'number' ? (isNaN(o) ? 'nan' : !isFinite(o) ? 'infinity' : t) : t
}

export const isUndefined = o => {
  return _type(o) === 'undefined'
}
export const isObject = o => {
  return _type(o) === 'object'
}


//首字母大写 _capitalize
export const _capitalize = (t) => {
  return t.slice(0, 1).toUpperCase() + t.slice(1)
}
//小驼峰
export const _camelCase = (arr) => {
  return arr.map((t, index) => index === 0 ? t : _capitalize(t)).join("")
}



// 判断属性
export const _has = (obj = {}, property = '') => {
  return Object.prototype.hasOwnProperty.call(obj, property)
}



export const deepClone = (obj) => {
  return JSON.parse(JSON.stringify(obj))
}


// 修改属性名
export const changeProperty = (obj, from, to) => {
  let target = deepClone(obj)
  if (!_has(target, to)) {
    target[to] = target[from]
    deleteProperty(target, from)
  }
  return target
}
// 删除属性
export const deleteProperty = (obj, property) => {
  delete obj[property]
  return obj
}


// foreach增加一个next参数
export const eachNext = (points, callback) => {
  let n = points.length
  points.forEach((t, index) => {
    let next = points[(index + 1) % n]
    callback && callback(t, index, next)
  })
}


export const plainTreeMenu = (tree) => {
  let list = []
  const fn = (t) => {
    t.forEach(t => {
      if (t.children) {
        fn(t.children)
      } else {
        list.push(t)
      }
    })
  }
  fn(tree)
  return list
}


// 分割符
export const propsSplitChar = "_"
export const getValue = (obj = {}, prop = "") => {
  let index = prop.indexOf(propsSplitChar)
  if (index === -1) {
    return obj ? obj[prop] : ''
  } else {
    return getValue(obj[prop.slice(0, index)], prop.slice(index + propsSplitChar.length))
  }
}
export const setValue = (obj, prop, value) => {
  let index = prop.indexOf(propsSplitChar)
  if (index === -1) {
    obj[prop] = value
  } else {
    if (!obj[prop.slice(0, index)]) {
      obj[prop.slice(0, index)] = {}
    }
    setValue(obj[prop.slice(0, index)], prop.slice(index + propsSplitChar.length), value)
  }
}


export const exportJSON = (data, name) => {
  let type = 'json'
  let link = document.createElement("a");
  let exportName = name ? name : 'data';
  let url = 'data:text/' + type + ';charset=utf-8,\uFEFF' + encodeURI(data);
  link.href = url;
  link.download = exportName + "." + type;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}

export const exportSVG = (data, name) => {
  let type = 'svg'
  let link = document.createElement("a");
  let exportName = name ? name : 'data';
  let url = 'data:text/' + type + ';charset=utf-8,\uFEFF' + encodeURI(data);
  link.href = url;
  link.download = exportName + "." + type;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}

export const exportCanvasAsPNG = (data, name) => {
  var MIME_TYPE = "image/png";
  var link = document.createElement('a');
  link.download = name;
  link.href = data
  link.dataset.downloadurl = [MIME_TYPE, link.download, link.href].join(':');

  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}


// 小驼峰命名法：camelCase
// 下划线命名法：snake_case
// 中划线命名法：kebab-case
export const camel2kebab = (key) => {
  return key.replace(/([A-Z])/g, (p, m) => `-${m.toLowerCase()}`)
}
// 将 camel 命名转 sanke 命名
export const camel2snake = (key) => {
  return key.replace(/([A-Z])/g, (p, m) => `_${m.toLowerCase()}`)
}
// 将 sanke 命名转 camel 命名
export const snake2camel = (key) => {
  return key.replace(/_([a-z])/g, (p, m) => m.toUpperCase())
}
